// pages/bookings/index.js
const app = getApp()
import Ajax from '../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    dataList: [],
    showBoolean: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getDateEvent()
  },

  returnPageEvent() {
    wx.switchTab({
      url: '/pages/me/index',
    })
  },
  async getDateEvent() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });
    try {
      let res = await Ajax.get('/wxa/visit/getList');
      let _list = res.result || [];
      wx.hideToast();
      for (let item of _list) {
        item.trangle = item.status == 1 ? true : false
        item.times = item.visitTime.split(' ')[0];
        item.default_visitTime = item.times + ' ' + item.startTime + ":00";
        item.day = 0;
        item.hours = 0;
        if (item.status == 1) {
          let time = item.default_visitTime.replace(/-/g, '/')
          let _time = Date.parse(time) - Date.parse(new Date());
          let getDays = this.getDays(_time / 1000);
          if (!getDays) {
            item.day = 0;
            item.hours = 0;
          } else {
            item.day = getDays.day;
            item.hours = getDays.hour;
          }
        }
      }
      this.setData({
        showBoolean: _list.length,
        dataList: _list
      })
    } catch (error) {
      wx.hideToast();
      console.log(error)
    }
  },
  getDays(second) { //返回天、时、分
    if (second > 0) {
      var day = 0;
      var hour = 0;
      var minute = 0;
      var data = {};
      minute = Math.floor(second / (60))
      if (parseInt(minute) > 60) {
        hour = parseInt(minute / 60);
        minute %= 60; //算出有多分钟
      }
      if (parseInt(hour) > 24) {
        day = parseInt(hour / 24);
        hour %= 24; //算出有多分钟
      }
      data.day = day;
      data.hour = hour;
      data.minute = minute;
      return data;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 点击三角表，收起或者展开
  bindToggleEvent(event) {
    let _item = event.currentTarget.dataset['item'];
    let _list = this.data.dataList;
    for (let item of _list) {
      if (item.id == _item.id) {
        item.trangle = !item.trangle;
      }
    }
    this.setData({
      dataList: _list
    })
  },
  cancelEvent(event) {
    let _item = event.currentTarget.dataset['item'];
    wx.showModal({
      title: '取消预约',
      content: '请确认是否取消预约？',
      success: async(res) => {
        if (res.confirm) {
          try {
            let res = await Ajax.get('/wxa/visit/cancelAppoint', {
              appointCode: _item.appointCode
            });
            if (res.code != 1) {
              wx.showToast({
                title: res.errorMsg,
                icon: 'none',
                duration: 2000
              })
              return false;
            }
            wx.showToast({
              title: '取消成功',
              duration: 1000
            });
            let _list = this.data.dataList;
            for (let item of _list) {
              if (item.id == _item.id) {
                item.status = 0;
              }
            }
            this.setData({
              dataList: _list
            })
          } catch (error) {
            console.log('取消失败')
            console.log(error)
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})